# Copyright (c) 2009 by Alex Leone <acleone ~at~ gmail.com>
#
# This file is part of epp.
#
# epp is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# epp is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with epp.  If not, see <http://www.gnu.org/licenses/>.

r"""
Contains the [filename] tag.

Syntax:
    [filename]       = output filename:
                          filename.ext
    [filename r]     = relative path from input directory:
                          dir/
    [filename rf]    = relative path and output filename:
                          dir/filename.ext
    [filename abs]   = absolute output path:
                          /...outputroot/dir/
    [filename absf]  = absolute output path with output filename:
                          /...outputroot/dir/filename.ext

    [filename i]     = input filename:
                          filename.ext.epp
    [filename ri]    = relative path and input filename:
                          dir/filename.ext.epp
    [filename absi]  = absolute input path:
                        /...inputroot/dir/
    [filename absif] = absolute input path with input filename:
                        /...inputroot/dir/filename.ext.epp

    [filename rabs]  = relative path to root (reverse of relative path):
                        ../../../

Outputs:
    see Syntax

Examples:
    Note: s[1:] in p.parsetags(s[1:]) is to strip the leading '\n'
          from s

    >>> import epp
    >>> import os
    >>> p = epp.Epp()
    >>> p.setfileroots('inroot' + os.sep, 'outroot' + os.sep)
    >>> relpath = os.path.join('topdir', 'subdir')
    >>> p.setrelpath(relpath)
    >>> p.setfilenames('sample.txt.epp', 'sample.txt')
    >>> print(p.parsetags('[filename]'))
    sample.txt
    >>> p.parsetags('[filename r]') == relpath
    True
    >>> p.parsetags('[filename rf]') == os.path.join(
    ...                                    relpath, 'sample.txt')
    True
    >>> p.parsetags('[filename absf]') == os.path.join(
    ...                                   'outroot',
    ...                                   relpath,
    ...                                   'sample.txt')
    True
"""

import os

import tags
from tags.tag import Tag

tagnames = ["filename"]

class Filename(Tag):

    def replace(self, eppinst, tagName, attrs):
        """
        Returns: output filename, eg filename.ext, unless attrs:
            'r'   - relative path
            'rf'  - relative path + output filename
            'abs' - absolute output file address
            'i'      - input filename
            'ri'  - relative path + input filename
            'absi'- absolute input file address
        """
        for attrName, value in attrs:
            if attrName == 'r':
                return eppinst.getrelpath()
            if attrName == 'rf':
                return os.path.join(eppinst.getrelpath(),
                                    eppinst.getoutname())
            if attrName == 'abs':
                return os.path.join(eppinst.getoutroot(),
                                    eppinst.getrelpath())
            if attrName == 'absf':
                return os.path.join(eppinst.getoutroot(),
                                    eppinst.getrelpath(),
                                    eppinst.getoutname())
            if attrName == 'i':
                return eppinst.getinname()
            if attrName == 'ri':
                return os.path.join(eppinst.getrelpath(),
                                    eppinst.getinname())
            if attrName == 'absi':
                return os.path.join(eppinst.getinroot(),
                                    eppinst.getrelpath())
            if attrName == 'absif':
                return os.path.join(eppinst.getinroot(),
                                    eppinst.getrelpath(),
                                    eppinst.getinname())
            if attrName == 'rabs':
                if len(eppinst.getrelpath()) == 0:
                    return '.'
                return os.sep.join('..' * eppinst.getrelpath().count(os.sep))
        return eppinst.getoutname()